home *** CD-ROM | disk | FTP | other *** search
- //****************************************************************************
- // File: glthread.c
- //
- // Purpose: Contains code to set up the pixel format and initialize OpenGL.
- // Also contains code to rotate and draw three-dimensional TrueType
- // characters from display lists created with wglUseFontOutlines().
- //
- // Development Team:
- // Greg Binkerd - Windows Developer Support
- //
- // Written by Microsoft Windows Developer Support
- // Copyright (c) 1995 Microsoft Corporation. All rights reserved.
- //****************************************************************************
-
- #include "glfont.h"
-
- // Handle to main window
- extern HWND ghWnd;
-
- // Boolean indicating if it is safe to draw or not
- extern BOOL bDraw;
-
- HPALETTE ghpalOld, ghPalette = (HPALETTE) 0;
- GLfloat radius;
- RECT oldrect;
-
- unsigned char threeto8[8] = {
- 0, 0111>>1, 0222>>1, 0333>>1, 0444>>1, 0555>>1, 0666>>1, 0377
- };
-
- unsigned char twoto8[4] = {
- 0, 0x55, 0xaa, 0xff
- };
-
- unsigned char oneto8[2] = {
- 0, 255
- };
-
- static int defaultOverride[13] = {
- 0, 3, 24, 27, 64, 67, 88, 173, 181, 236, 247, 164, 91
- };
-
- static PALETTEENTRY defaultPalEntry[20] = {
- { 0, 0, 0, 0 },
- { 0x80,0, 0, 0 },
- { 0, 0x80,0, 0 },
- { 0x80,0x80,0, 0 },
- { 0, 0, 0x80, 0 },
- { 0x80,0, 0x80, 0 },
- { 0, 0x80,0x80, 0 },
- { 0xC0,0xC0,0xC0, 0 },
-
- { 192, 220, 192, 0 },
- { 166, 202, 240, 0 },
- { 255, 251, 240, 0 },
- { 160, 160, 164, 0 },
-
- { 0x80,0x80,0x80, 0 },
- { 0xFF,0, 0, 0 },
- { 0, 0xFF,0, 0 },
- { 0xFF,0xFF,0, 0 },
- { 0, 0, 0xFF, 0 },
- { 0xFF,0, 0xFF, 0 },
- { 0, 0xFF,0xFF, 0 },
- { 0xFF,0xFF,0xFF, 0 }
- };
-
- //***********************************************************************
- // Function: ComponentFromIndex
- //
- //
- // Comments: Taken from the "GenGL" OpenGL sample application
- //
- //****************************************************************************
- unsigned char ComponentFromIndex(int i, UINT nbits, UINT shift)
- {
- unsigned char val;
-
- val = (unsigned char) (i >> shift);
- switch (nbits) {
-
- case 1:
- val &= 0x1;
- return oneto8[val];
-
- case 2:
- val &= 0x3;
- return twoto8[val];
-
- case 3:
- val &= 0x7;
- return threeto8[val];
-
- default:
- return 0;
- }
- }
-
- //***********************************************************************
- // Function: CreateRGBPalette
- //
- //
- // Comments: Taken from the "GenGL" OpenGL sample application
- //
- //****************************************************************************
- void CreateRGBPalette(HDC hDC)
- {
- PIXELFORMATDESCRIPTOR pfd;
- LOGPALETTE *pPal;
- int n, i;
-
- n = GetPixelFormat(hDC);
- DescribePixelFormat(hDC, n, sizeof(PIXELFORMATDESCRIPTOR), &pfd);
-
- if (pfd.dwFlags & PFD_NEED_PALETTE) {
- n = 1 << pfd.cColorBits;
- pPal = (PLOGPALETTE)LocalAlloc(LMEM_FIXED, sizeof(LOGPALETTE) +
- n * sizeof(PALETTEENTRY));
- pPal->palVersion = 0x300;
- pPal->palNumEntries = n;
- for (i=0; i<n; i++) {
- pPal->palPalEntry[i].peRed =
- ComponentFromIndex(i, pfd.cRedBits, pfd.cRedShift);
- pPal->palPalEntry[i].peGreen =
- ComponentFromIndex(i, pfd.cGreenBits, pfd.cGreenShift);
- pPal->palPalEntry[i].peBlue =
- ComponentFromIndex(i, pfd.cBlueBits, pfd.cBlueShift);
- pPal->palPalEntry[i].peFlags = 0;
- }
-
- /* fix up the palette to include the default GDI palette */
- if ((pfd.cColorBits == 8) &&
- (pfd.cRedBits == 3) && (pfd.cRedShift == 0) &&
- (pfd.cGreenBits == 3) && (pfd.cGreenShift == 3) &&
- (pfd.cBlueBits == 2) && (pfd.cBlueShift == 6)
- ) {
- for (i = 1 ; i <= 12 ; i++)
- pPal->palPalEntry[defaultOverride[i]] = defaultPalEntry[i];
- }
-
- ghPalette = CreatePalette(pPal);
- LocalFree(pPal);
-
- ghpalOld = SelectPalette(hDC, ghPalette, FALSE);
- n = RealizePalette(hDC);
- }
- }
-
- //***********************************************************************
- // Function: bSetupPixelFormat
- //
- //
- // Comments: Taken from the "GenGL" OpenGL sample application
- //
- //****************************************************************************
- BOOL bSetupPixelFormat(HDC hDC)
- {
- static PIXELFORMATDESCRIPTOR pfd = {
- sizeof(PIXELFORMATDESCRIPTOR), // size of this pfd
- 1, // version number
- PFD_DRAW_TO_WINDOW | // support window
- PFD_DOUBLEBUFFER |
- PFD_SUPPORT_OPENGL, // support OpenGL
- PFD_TYPE_RGBA, // RGBA type
- 24, // 24-bit color depth
- 0, 0, 0, 0, 0, 0, // color bits ignored
- 0, // no alpha buffer
- 0, // shift bit ignored
- 0, // no accumulation buffer
- 0, 0, 0, 0, // accum bits ignored
- 32, // 32-bit z-buffer
- 0, // no stencil buffer
- 0, // no auxiliary buffer
- PFD_MAIN_PLANE, // main layer
- 0, // reserved
- 0, 0, 0 // layer masks ignored
- };
- int pixelformat;
-
- if ( (pixelformat = ChoosePixelFormat(hDC, &pfd)) == 0 )
- {
- MessageBox(NULL, "ChoosePixelFormat failed", "Error", MB_OK);
- return FALSE;
- }
-
- if (SetPixelFormat(hDC, pixelformat, &pfd) == FALSE)
- {
- MessageBox(NULL, "SetPixelFormat failed", "Error", MB_OK);
- return FALSE;
- }
-
- CreateRGBPalette(hDC);
-
- return TRUE;
- }
-
-
- //***********************************************************************
- // Function: initialize
- //
- // Purpose: Called by Windows on app startup. Initializes everything,
- // and enters a message loop.
- //
- // Parameters:
- // hWnd == Handle to window
- //
- // Returns: none
- //
- // Comments:
- //
- // History: Date Author Reason
- // 3/20/95 GGB Created
- //****************************************************************************
-
- GLvoid initialize(HWND hWnd)
- {
- GLfloat maxObjectSize, aspect;
- GLdouble near_plane, far_plane;
-
- GetClientRect(hWnd, &oldrect);
- glClearColor( 0.0, 0.0, 0.0, 1.0 );
-
- glEnable(GL_DEPTH_TEST);
-
- glMatrixMode( GL_PROJECTION );
- aspect = (GLfloat) oldrect.right / oldrect.bottom;;
- gluPerspective( 15.0, aspect, 2.0, -2.0 );
- glMatrixMode( GL_MODELVIEW );
-
- near_plane = 2.0;
- far_plane = -2.0;
- maxObjectSize = 2.0;
- radius = near_plane + maxObjectSize/2.0;
- }
-
-
-
- //***********************************************************************
- // Function: draw_scene
- //
- // Purpose: Called when a WM_TIMER message is sent to the main window. It
- // will draw three-dimensional TrueType characters from display
- // lists created with wglUseFontOutlines().
- //
- // Parameters: hWnd == main window handle
- //
- // Returns: none
- //
- // Comments:
- //
- // History: Date Author Reason
- // 5/31/95 GGB Created
- //****************************************************************************
-
- GLvoid draw_scene(HWND hWnd)
- {
- HDC hdc;
- // Static GLfloat's to hold the current rotation handles
- static GLfloat fAngY = 5.0f;
- static GLfloat fAngX = 1.0f;
- static GLfloat fAngZ = 3.0f;
-
- // We don't want re-entrancy
- bDraw = FALSE;
-
- // Increase the angles so we can rotate the text
- fAngX += 5.0f;
- fAngY += 5.0f;
- fAngZ += 2.0f;
-
- // Clear the OpenGL window's client area
- glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
- glPushMatrix();
- glTranslatef(0.0, 0.0, -radius);
- // rotate
- glRotatef(fAngX, 1.0f, 0.0f, 0.0f);
- glRotatef(fAngY, 0.0f, 1.0f, 0.0f);
- glRotatef(fAngZ, 0.0f, 0.0f, 1.0f);
-
- glScalef(0.3f, 0.3f, 0.3f);
-
-
- // Display a string with the display lists created by wglUseFontOutlines()
- glColor3f(1.0,0.5,0.5);
- glListBase(GLF_START_LIST); // indicate the start of display lists for the glyphs.
- // Draw the characters
- glCallLists(6, GL_UNSIGNED_BYTE, "OpenGL");
-
- glPopMatrix();
- glFinish();
- // We are double-buffering, so we need to swap the buffers
- hdc = wglGetCurrentDC();
- SwapBuffers(hdc);
- bDraw = TRUE;
- }
-
-
- //***********************************************************************
- // Function: resize
- //
- //
- // Comments: Taken from the "GenGL" OpenGL sample application. Modified to
- // not generate a WM_PAINT message.
- //
- //****************************************************************************
- GLvoid resize(HWND hWnd)
- {
- RECT rect;
-
- GetClientRect(hWnd, &rect);
-
- glViewport(0, 0, rect.right, rect.bottom);
-
- oldrect.right = rect.right;
- oldrect.bottom = rect.bottom;
- }
-
-
-